<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="部门ID" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="工序"  prop="processId">
            <el-select  v-model="queryParams.processId" placeholder="请选择工序" filterable clearable >
            <el-option v-for="item in processList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
          </el-form-item> -->
          
      <el-form-item label="组别" prop="groupId">
        <el-select v-model="queryParams.groupId" placeholder="请选择组别" clearable>
          <el-option v-for="dict in groups" :key="dict.id" :label="dict.groupName"
            :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="人员名称" prop="employeeId">
        <el-input
          v-model="queryParams.employeeId"
          placeholder="请输入人员名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="工种" prop="workerType">
        <el-select v-model="queryParams.workerType" placeholder="请选择人员状态" clearable>
          <el-option v-for="dict in wokerTypes" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="人员状态" prop="peopleState">
        <el-select v-model="queryParams.peopleState" placeholder="请选择人员状态" clearable>
          <el-option v-for="dict in peopleStates" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="人员状态" prop="peopleState">
        <el-input
          v-model="queryParams.peopleState"
          placeholder="请输入人员状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="state == 0">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['zndd:processdetails:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['zndd:processdetails:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zndd:processdetails:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['zndd:processdetails:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="processdetailsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <!-- <el-table-column label="部门ID" align="center" prop="deptId" /> -->
      <!-- <el-table-column label="工序" align="center" prop="processId" :formatter="peocessFomatter"/> -->
      <el-table-column label="组别" align="center" prop="groupId"  :formatter="groupFomatter"/>
      <el-table-column label="人员名称" align="center" prop="employeeId"  :formatter="employeeFomatter"/>
      <!-- <el-table-column label="工种" align="center" prop="workerType"  :formatter="wokerTypeFomatter"/> -->
      <el-table-column label="人员状态" align="center" prop="peopleState"  :formatter="peopleStateFomatter"/>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope"  v-if="state == 0">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['zndd:processdetails:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['zndd:processdetails:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工序详情信息（增删人员需要新增数据）对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="部门ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门ID" />
        </el-form-item>
        <el-form-item label="工序ID" prop="processId">
          <el-input v-model="form.processId" placeholder="请输入工序ID" />
        </el-form-item> -->
        <el-form-item label="组别" prop="groupId">
          <el-select v-model="form.groupId" placeholder="请选择组别" @change="changGroup()">
            <el-option v-for="dict in groups" :key="dict.id" :label="dict.groupName" :value="dict.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="人员" prop="employeeId">
          <el-select v-model="form.employeeId" placeholder="请选择人员">
            <el-option v-for="dict in selectEmplpyees" :key="dict.id" :label="dict.employeeName" :value="dict.id" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="人员状态" prop="peopleState">
          <el-select v-model="form.peopleState" placeholder="请选择人员状态">
            <el-option v-for="dict in peopleStates" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
          </el-select>
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProcessdetails, getProcessdetails, delProcessdetails, addProcessdetails, updateProcessdetails } from "@/api/zndd/process/processdetails";
import { listAllprocess } from "@/api/zndd/process/process";
import { getDicts } from "@/api/system/dict/data";
import { listGroup } from "@/api/zndd/rest/group";
import { listEmployee} from "@/api/zndd/rest/employee";

export default {
  name: "Processdetails",
  data() {
    return {
      //从工位界面接收到的工位状态
      state:0,
      //从工位界面接收的工位数据
      satation:null,
      //组别列表
      groups: [],
      //人员列表
      employees:[],
      //筛选后人员列表
      selectEmplpyees:[],
      //工种列表
      wokerTypes:[],
      //人员状态列表
      peopleStates:[],
      //存储工序列表
      processList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工序详情信息（增删人员需要新增数据）表格数据
      processdetailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        stationId: null,
        groupId:null,
        employeeId: null,
        workerType: null,
        peopleState: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        employeeId: [
          { required: true, message: "人员不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    var param = {
      pageSize: 1000,
    }
    //获取组别数据
    listGroup(param).then(response => {
      this.groups = response.rows;
    });
     //获取人员数据
     listEmployee(param).then(response => {
      this.employees = response.rows;
    });
    //获取字典表中工种类型
    this.getDicts("zndd_job_type").then(response => {
      this.wokerTypes = response.data;
    });
    //获取字典表中人员状态
    this.getDicts("zndd_process_people_state").then(response => {
      this.peopleStates = response.data;
    });
    //获取工序列表
    listAllprocess().then(response => {
      var list = [];
        response.rows.forEach(row => {
          list.push({
            label: row.processName,
            value: row.id
          })
        });
      this.processList = list;
    });
    //this.getList();
  },
  methods: {
    //修改组别后人员展示
    changGroup(){
      var param = {
        pageSize: 1000,
        peopleState: 0,
      }
      listProcessdetails(param).then(response => {
        var list = [];
        this.employees.forEach(employee => {
          if(employee.groupId == this.form.groupId){
            var flag = 1;
            response.rows.forEach(processDetails => {
              if(processDetails.employeeId == employee.id){
                flag=2;
              }
            });
            if(flag == 1){
              list.push(employee);
            };
          }
        });
        this.selectEmplpyees = list;
      })
    },
    //从工位界面接收数据
    pdMethods(satation) {
      if (satation != null && satation != "undefind") {
        this.satation = satation;
      };
      if(satation.stationState != null){
        this.state = satation.stationState;
      }
      this.queryParams.stationId = satation.id;
      this.getList();
    },
    //格式化人员
    employeeFomatter(row, column) {
      var ee = null;
      this.employees.forEach(employee => {
        if (employee.id == row.employeeId) {
          ee = employee;
        }
      });
      if(ee == null){
        return "";
      }else{
        return ee.employeeName;
      }
    },
    //格式化组别
    groupFomatter(row, column) {
      var gp = null;
      this.groups.forEach(group => {
        if (group.id == row.groupId) {
          gp = group;
        }
      });
      if(gp == null){
        return "";
      }else{
        return gp.groupName;
      }
    },
    //格式化人员状态
    peopleStateFomatter(row, column) {
      var ps = null;
      this.peopleStates.forEach(peopleState => {
        if (peopleState.dictValue == row.peopleState) {
          ps = peopleState;
        }
      });
      if(ps == null){
        return "";
      }else{
        return ps.dictLabel;
      }
    },
    //格式化工种
    wokerTypeFomatter(row, column) {
      var wt = null;
      this.wokerTypes.forEach(wokerType => {
        if (wokerType.dictValue == row.workerType) {
          wt = wokerType;
        }
      });
      if(wt == null){
        return "";
      }else{
        return wt.dictLabel;
      }
    },
    //格式化工序
    peocessFomatter(row, column){
      var pc = null;
      this.processList.forEach(peocess => {
        if (peocess.value == row.processId) {
          pc = peocess;
        }
      });
      if(pc == null){
        return "";
      }else{
        return pc.label;
      }
    },
    /** 查询工序详情信息（增删人员需要新增数据）列表 */
    getList() {
      this.loading = true;
      listProcessdetails(this.queryParams).then(response => {
        this.processdetailsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        processId: null,
        employeeId: null,
        workerType: null,
        peopleState: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.selectEmplpyees = [];
      this.open = true;
      this.form.stationId = this.satation.id;
      this.form.peopleState = this.state;
      this.title = "添加工序详情信息（增删人员需要新增数据）";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProcessdetails(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工序详情信息（增删人员需要新增数据）";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProcessdetails(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProcessdetails(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的人员信息数据项？').then(function() {
        return delProcessdetails(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zndd/processdetails/export', {
        ...this.queryParams
      }, `工序人员信息_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
